<?php
class SysPhotoBusi {
	/**
	 * 上传图片
	 */
	public function uploadPhoto($condition) {
		global $mysqlTools;
		
		// 生成唯一ID
		$uniqid = getCurrentId ();
		
		// 参数处理注入SQL安全问题
		$condition = $mysqlTools->check_input ( $condition );
		try {
			$mysqlTools->startTransaction ();
			$result = $mysqlTools->executeSql ( "
                INSERT INTO sys_photo(
                	photoId,
                	photoName,
                	userId,
                	photoAlbumId,
                	photoUrl,
                	photoCreateDate,
                	photoState,
                	photoWidth,
                	photoHeight,
                	photoSize,
	                photoTitle,
					photoNoExtUrl,
					photoIsOffic,
					photoType,
					photoExt
                ) VALUES(
                	$uniqid,
	                $condition[photoName],
	                $condition[userId],
	                $condition[photoAlbumId],
	                $condition[photoUrl],
	                now(),
	                1,
	                $condition[photoWidth],
	                $condition[photoHeight],
	                $condition[photoSize],
	                $condition[photoTitle],
					$condition[photoNoExtUrl],
					$condition[photoIsOffic],
					$condition[photoType],
					$condition[photoExt]
                )
	        " );
			$mysqlTools->commitTransaction ();
		} catch ( \Exception $e ) {
			$mysqlTools->rollback ();
			return "数据库插入发生异常";
		}
		if ($result)
			return $uniqid;
		else
			return $result;
	}
	
	// 通过相册ID查询相册图片信息
	public function queryPhotosByAlbumId($condition) {
		global $mysqlTools;
		
		$condition = $mysqlTools->check_input ( $condition );
		$userId = $_SESSION ['userInfo'] ['userId'];
		$key = "";
		// 过滤关键字
		if (isset ( $condition ['key'] ) && is_null ( $condition ['key'] ))
			$key = "and sp.photoTitle like '%$condition[key]%'";
			
			// 查询用户相册信息
		$photos = $mysqlTools->selectSql ( "
	            SELECT
                	sp.photoId,
                	sp.photoName,
	                sp.photoTitle,
                	sp.userId,
                	sp.photoAlbumId,
                	sp.photoUrl,
                	sp.photoCreateDate,
                	sp.photoState,
                	sp.photoWidth,
                	sp.photoHeight,
                	sp.photoSize
                FROM
                	sys_photo sp
                WHERE
                	sp.photoAlbumId = $condition[albumId] and
	                sp.userId = 1000000000000154  $key
	            " );
		
		return $photos;
	}
	
	// 根据图片ID删除该图片信息
	public function delPhotoByPhotoId($photoIds) {
		global $mysqlTools;
		$result = false;
		if (! empty ( $photoIds )) {
			try {
				$mysqlTools->startTransaction ();
				$result = $this->delPhotoByPhotoIds ( $photoIds );
				if ($result)
					$mysqlTools->commitTransaction ();
				else
					$mysqlTools->rollback ();
			} catch ( \Exception $e ) {
				$mysqlTools->rollback ();
				return false;
			}
		}
		return $result;
	}
	
	// 根据图片ID删除该图片信息
	public function delPhotoByPhotoIds($photoIds) {
		global $mysqlTools;
		$list = explode ( ',', $photoIds );
		$result = true;
		if (! empty ( $list )) {
			try {
				foreach ( $list as $photoId ) {
				    $photoUrl = $mysqlTools->selectSql ("
				    SELECT
				    sp.photoUrl
				    FROM
				    sys_photo sp
				    WHERE
				    sp.photoId = $photoId 
				    ")[0]['photoUrl'];
				    unlink($_SERVER["DOCUMENT_ROOT"] . iconv('utf-8', 'gbk', $photoUrl));
					$result = $mysqlTools->executeSql ( "
							delete from sys_photo where photoId = $photoId
							" );
					if (!$result) {
						return false;
					}
				}
			} catch ( \Exception $e ) {
				return false;
			}
		}
		return $result;
	}
}

?>